<!DOCTYPE html>
<html>
<body>
<div>This tests the basic functionality and properties of DataTransferItems for files with drag and drop. This test requires DRT.</div>

<div id="destination" style="min-height:100px; border: solid 1px black">Drop files here if you test this manually</div>

<div id="console"></div>

<script>
var testFiles = [
  { path: 'resources/mozilla.gif',
    directory: false,
    size: 2593 },
  { path: 'resources/drop-file-svg.svg',
    directory: false,
    size: 109 },
  { path: 'resources/copy-backslash-euc.html',
    directory: false,
    size: 478 },
  { path: 'resources/test_directory',
    directory: true,
    size: 0 }
];

function log(text)
{
    var console = document.getElementById('console');
    console.appendChild(document.createTextNode(text));
    console.appendChild(document.createElement('br'));
}

function test(expect, actual)
{
    log((expect == actual ? 'PASS' : 'FAIL') + ': "' + expect + '" == "' + actual + '"');
}

function startTest()
{
    var destination = document.getElementById('destination');
    destination.addEventListener('dragover', handleDragOver, false);
    destination.addEventListener('drop', handleDrop, false);

    if (!window.testRunner)
        return;
    testRunner.waitUntilDone();
    testRunner.dumpAsText();

    var files = [];
    for (var i = 0; i < testFiles.length; ++i) {
      log('Dragging file: ' + testFiles[i].path);
      files.push(testFiles[i].path);
    }

    // Perform drag-and-drop with the testFiles.
    eventSender.beginDragWithFiles(files);
    eventSender.leapForward(100);
    eventSender.mouseMoveTo(destination.offsetLeft + 10, destination.offsetTop + destination.offsetHeight / 2);
    eventSender.mouseUp();
}

function handleDragOver(e)
{
    e.stopPropagation();
    e.preventDefault();
}

function handleDrop(e)
{
    e.stopPropagation();
    e.preventDefault();

    log('Verifying contents of DataTransferItems...');
    var items = e.dataTransfer.items;
    var files = [];
    test(testFiles.length, items.length);
    for (var i = 0; i < items.length; ++i) {
        // The items should be in the same order as we added.
        var expected = testFiles[i];
        var file = items[i].getAsFile();
        files.push(file);

        test('file', items[i].kind);
        var entry = items[i].webkitGetAsEntry();
        log('entry: ' + entry.fullPath + (entry.isDirectory ? ' [dir]' : ' [file]'));

        var components = expected.path.split('/');
        var name = components[components.length - 1];
        test('/' + name, entry.fullPath);
        test(expected.directory, entry.isDirectory);

        with ({last: i + 1 == items.length, expected: expected}) {
            entry.getMetadata(function(metadata) {
                if (!expected.directory)
                    test(expected.size, metadata.size);
                if (last && window.testRunner)
                    testRunner.notifyDone();
            });
        }
    }
}

startTest();

</script>
</body>
</html>
